/********************************************
 * 功能说明: 
 * 模块名称: 
 * 系统名称: 
 * 软件版权: 北京银杉金服科技有限公司
 * 系统版本: 1.0.0
 * 开发人员: zhangfb
 * 开发时间: 2019/1/12 22:20
 * 审核人员: 
 * 相关文档: 
 * 修改记录: 修改日期 修改人员 修改说明
 *********************************************/
package com.hyacinth.server.action.response;

import com.hyacinth.server.action.request.Cookie;
import com.hyacinth.server.constant.Constant;
import com.hyacinth.server.util.Assert;
import io.netty.handler.codec.http.cookie.DefaultCookie;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * http响应对象
 * @author zhangfb
 * @version 1.0.0.1
 * @since JDK 1.8
 */
public class AuroraHttpResponse implements AuroraResponse {

    private AuroraHttpResponse() {}

    private Map<String, String> headers = new HashMap<>(8);

    private String contentType;

    private String httpContent;

    private List<io.netty.handler.codec.http.cookie.Cookie> cookies = new ArrayList<>(6);

    /**
     * 初始化http响应
     * @return 响应实例
     */
    public static AuroraHttpResponse init() {
        AuroraHttpResponse response = new AuroraHttpResponse();
        response.contentType = Constant.ContentType.TEXT;
        return response;
    }

    @Override
    public Map<String, String> getHeaders() {
        return this.headers;
    }

    public void setHeaders(String key, String value) {
        this.headers.put(key, value);
    }

    @Override
    public void setContentType(String contentType) {
        this.contentType = contentType;
    }

    @Override
    public String getContentType() {
        return contentType;
    }

    @Override
    public void setHttpContent(String content) {
        this.httpContent = content;
    }

    @Override
    public String getHttpContent() {
        return this.httpContent == null ? "" : this.httpContent;
    }

    @Override
    public void setCookie(Cookie auroraCookie) {
        Assert.notNull(auroraCookie, "auroraCookie is null!");
        Assert.notNull(auroraCookie.getName(), "auroraCookie.getName() is null!");
        Assert.notNull(auroraCookie.getValue(), "auroraCookie.getValue() is null!");

        DefaultCookie cookie = new DefaultCookie(auroraCookie.getName(), auroraCookie.getValue());
        cookie.setPath("/");
        cookie.setMaxAge(auroraCookie.getMaxAge());

        cookies.add(cookie) ;
    }

    @Override
    public List<io.netty.handler.codec.http.cookie.Cookie> cookies() {
        return this.cookies;
    }
}
